package cn.itcast.backend1;


import cn.itcast.backend3.ObjectUtil;
import cn.itcast.pojo.Person;
import cn.itcast.pojo.Address;

import java.security.InvalidParameterException;


public class backend1 {
    public static void main(String[] args) {
        // Test1: 复制基本数据类型和字符串
        Person sourcePerson = new Person();
//        sourcePerson.setName("Alice");
//        sourcePerson.setAge(30);
//        Person targetPerson = new Person();
////        String[] properties = {"name", "age"};
//        String[] properties = {};
//        if (properties.length == 0) {
//            throw new InvalidParameterException("properties array is empty");
//        }
//        ObjectUtil.copyProperties(sourcePerson, targetPerson, properties);
//        System.out.println("test1: " + targetPerson.getName() + " " + targetPerson.getAge()); //test1: Alice 30

        // Test2: 复制自定义对象
        Address sourceAddress = new Address();
//        sourceAddress.setStreet("Maple Street");
//        Address targetAddress = new Address();
//        ObjectUtil.copyProperties(sourceAddress, targetAddress, new String[]{"street"});
//        System.out.println("test1: " + targetAddress.getStreet());

        // Test3: 复制具有循环引用的对象
//        sourcePerson.setAddress(sourceAddress);
//        sourceAddress.setOwner(sourcePerson); //循环引用
//        Person copiedPerson = new Person();
//        Address copiedAddress = new Address();
//        copiedPerson.setAddress(copiedAddress);
//        ObjectUtil.copyProperties(sourcePerson, copiedPerson, new String[]{"name", "age", "address"});
//        System.out.println("test3: "+copiedPerson.getName()+", "+copiedPerson.getAge()+", "+copiedPerson.getAddress());

        // Test4: 使用不匹配的属性类型进行复制
//        try {
//            ObjectUtil.copyProperties(sourcePerson, new Object(), new String[]{"name"});
//        } catch (RuntimeException e) {
//            throw new RuntimeException("类型不一致");
//        }

        // Test5: 属性数组为空或null
//        try {
//            ObjectUtil.copyProperties(sourcePerson, targetPerson, new String[]{});
//        } catch (InvalidParameterException e) {
//            throw new InvalidParameterException("properties array is empty");
//        }
//
//        try {
//            ObjectUtil.copyProperties(sourcePerson, targetPerson, null);
//        } catch (InvalidParameterException e) {
//            throw new InvalidParameterException("properties array is null");
//        }
    }
}